package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xquery.XQException;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import domain.Movie;

import entity.User;
import entity.Watched;
import ex.PlaceNotFoundException;

import service.WatchedFilmService;
import util.WeatherUtil;
import util.WebUtil;
import util.XmlUtil;

@WebServlet(urlPatterns={"/recommend"})
public class Recommend extends HttpServlet {
	WatchedFilmService wservice = new WatchedFilmService();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		if(session.getAttribute("user")!=null){			
			RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/recommend.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
		else{
			response.sendRedirect("");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		if(session.getAttribute("user")!=null){		
			User user=(User)session.getAttribute("user");
			String city=request.getParameter("city");
			try {
				if(city.equals(""))
					throw new Exception("City name must be entered");
				ArrayList<Watched> watched=wservice.getWatchedFilmsByUser(user);
				ArrayList<String> genres=new ArrayList<String>();
				double degree=WeatherUtil.getDegree(city);
				request.setAttribute("degree", degree);
				if(degree>=10){
					genres.add("comedy");
					genres.add("romance");
				}
				else if(degree<10 && degree>=5){
					genres.add("action");
				}
				else{
					genres.add("drama");
				}
				String domainName=WebUtil.getURLWithContextPath(request);
				Movie movie= getRecommendation(genres, watched, domainName);
				request.setAttribute("movie", movie);
				request.setAttribute("recommend", "done");
				
				RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/recommend.jsp");
				if (dispatcher != null) dispatcher.forward(request, response);
			}
			catch (Exception e) {
				request.setAttribute("error", e.getMessage());
				
				RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/recommend.jsp");
				if (dispatcher != null) dispatcher.forward(request, response);
			} 
		}
		else{
			response.sendRedirect("");
		}
		
	}

	private Movie getRecommendation(ArrayList<String> genres,ArrayList<Watched> watched,String domainName) throws Exception {
		return XmlUtil.getRandomRecommendation(genres, watched, domainName);
	}
}
